package com.example.aabb;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;

import java.util.ArrayList;
import java.util.List;

public class Page1Fragment extends Fragment {

    private final String[] months = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};
    private final String[] years = {"2024年", "2023年", "2022年"};
    private List<Bill2> billList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_page1, container, false);
        initMonthlyBillViews(rootView);
        return rootView;
    }

    private void initMonthlyBillViews(View view) {

        BarChart barChart = view.findViewById(R.id.bar_chart);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        TextView totalAmountText = view.findViewById(R.id.total_amount_text);
        Spinner yearSpinner = view.findViewById(R.id.year_spinner);
        Spinner monthSpinner = view.findViewById(R.id.month_spinner);
        Button expensesButton = view.findViewById(R.id.expenses_button);
        Button incomeButton = view.findViewById(R.id.income_button);
        Button otherButton = view.findViewById(R.id.other_button);
        // 设置年份和月份的Spinner
        ArrayAdapter<String> yearAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, years);
        yearSpinner.setAdapter(yearAdapter);
        ArrayAdapter<String> monthAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, months);
        monthSpinner.setAdapter(monthAdapter);

        // 模拟账单数据
        billList.add(new Bill2(R.mipmap.ic_launcher, "转账", -690.00,"一月"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "手机充值", -600.00,"二月"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "微信红包", -200.00,"三"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "Pay - 猪角", -127.50,"四月"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "京东商城平台商户", -100.00,"五月"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "中铁网络", -84.00,"六月"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "新华都", -63.69,"七月"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "邹公卤面沙茶面", -20.00,"八月"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "二维码收款", 13.50,"九月"));
        billList.add(new Bill2(R.mipmap.ic_launcher, "滴滴出行", 10.44,"十月"));

        // 设置RecyclerView布局管理器和适配器
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        BillAdapter adapter = new BillAdapter(billList);
        recyclerView.setAdapter(adapter);

        // 设置支出按钮点击事件
        expensesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 根据支出数据生成柱状图
                BarChartHelper.generateBarChart(barChart, billList, "支出");
            }
        });

        // 设置收入按钮点击事件
        incomeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 根据收入数据生成柱状图
                BarChartHelper.generateBarChart(barChart, billList, "收入");
            }
        });

        // 设置其他按钮点击事件（这里暂未实现具体逻辑）
        otherButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 可根据需求添加逻辑
            }
        });
    }

    private void initYearlyBillViews(View view) {
        TextView totalAmountText = view.findViewById(R.id.total_amount_text);
        // 这里可根据年账单的需求进一步完善初始化操作
        totalAmountText.setText("共支出614笔，合计：￥50411.50");
    }
}